<div class="form_content">
  <textarea id="artifact_template_build" class="template">
    <%= render :partial => 'builtin_artifact_form', :locals => {:scope => {:var_form => scope[:form], :builtin_artifact => com.thoughtworks.go.config.BuildArtifactConfig.new}} %>
  </textarea>

  <textarea id="artifact_template_test" class="template">
    <%= render :partial => 'builtin_artifact_form', :locals => {:scope => {:var_form => scope[:form], :builtin_artifact => com.thoughtworks.go.config.TestArtifactConfig.new}} %>
  </textarea>

  <textarea id="artifact_template_external" class="template">
    <%= render :partial => 'external_artifact_form', :locals => {:scope => {:var_form => scope[:form], :external_artifact => com.thoughtworks.go.config.PluggableArtifactConfig.new, :artifact_meta_data_store => scope[:artifact_meta_data_store], :artifact_plugin_to_view => scope[:artifact_plugin_to_view], :plugin_name_to_id => scope[:plugin_name_to_id], :store_id_to_plugin_id => scope[:store_id_to_plugin_id]}} %>
  </textarea>

  <div class="artifact-container">
    <% scope[:built_in_collection].each do |artifact| -%>
      <%= render :partial => 'builtin_artifact_form', :locals => {:scope => {:var_form => scope[:form], :builtin_artifact => artifact}} %>
    <%- end -%>
  </div>

  <% unless @is_config_repo_pipeline %>
    <div class="row expanded add_new_artifact">
    <div class="name_value_cell columns medium-12 large-12 end">
      <label>Select type:</label>
      <select class="small" id="select_artifact_type">
        <option value="Build Artifact">Build</option>
        <option value="Test Artifact">Test</option>
        <option value="External Artifact">External</option>
      </select>
      <%= action_icon({:type => "add", :text => 'Add', :href => "#", :class => "skip_dirty_stop add_artifact_button",
                       :id => "add_artifact"}) %>
    </div>
  </div>
  <% end %>
</div>
<script type="text/javascript">
  var artifactTypeToTemplate = {
    'Build Artifact':    '#artifact_template_build',
    'Test Artifact':     '#artifact_template_test',
    'External Artifact': '#artifact_template_external'
  };

  function determineAppropriateFormToShow() {
    var artifactType    = jQuery('#select_artifact_type').val();
    var publishArtifact = new PublishArtifact(artifactType, artifactTypeToTemplate[artifactType], {
      storeIds: <%== scope[:artifact_store_ids] %>,
      storeIdToPluginId: <%== scope[:store_id_to_plugin_id].to_json %>,
      artifactPluginToView: <%== scope[:artifact_plugin_to_view].to_json%>
    });
    publishArtifact.renderView();
  }

  Util.on_load(function () {

    <% scope[:external_artifact_collection].each do |external_artifact| %>
    new PublishArtifact("External Artifact", "#artifact_template_external", {
      artifactId:    "<%= external_artifact.getId() %>",
      storeId:       "<%= external_artifact.getStoreId() %>",
      storeIds: <%== scope[:artifact_store_ids] %>,
      storeIdToPluginId: <%== scope[:store_id_to_plugin_id].to_json %>,
      artifactPluginToView: <%== scope[:artifact_plugin_to_view].to_json%>,
      configuration: JSON.stringify(<%== com.google.gson.Gson.new.toJson(external_artifact.getConfiguration().getConfigWithErrorsAsMap()) %>),
      keyValuePairConfiguration: <%== com.google.gson.Gson.new.toJson(external_artifact.getConfiguration().getPropertyMetadataAndValuesAsMap()) %>
    }).renderView();
    <%- end -%>

    jQuery('#add_artifact').click(determineAppropriateFormToShow);
    jQuery('.delete_artifact').click(function (event) {
      jQuery(this).closest('.artifact').remove();
    });
  });
</script>
